import { getLodop } from '@/utils/LodopFuncs'
export function printInfo(requestList){
  let temp2
  let stemp='<style>table,th{border:none;height:28px} td{border:1px;border-width:1px;border-style:solid;border-collapse:collapse;}</style>'
  temp2 = '<DIV id="div_0_2"><TABLE border=0 cellSpacing=0 cellPadding=1 width="100%" style="border-collapse:collapse" bordercolor="#333333">'
  //temp2 = '<TABLE border=1 cellSpacing=0 cellPadding=1 width="100%" style="border-collapse:collapse" bordercolor="#333333">'
  temp2+='<thead>'
  temp2+='<tr>'
  temp2+='<th colspan="3" align="left" style="padding-left:45px;font-size:14px"><font color="#0000FF"><b>Order：</b><SPAN>'+requestList.orderSn+'</SPAN></font></th>'
  temp2+='<th colspan="4" align="left" style="padding-left:45px;font-size:14px"><font color="#0000FF"><b>OrderDate：</b><SPAN>'+requestList.orderCreateTime+'</SPAN></font></th>'
  temp2+='</tr>'
  temp2+='<tr><th colspan="3" align="left" style="word-break: break-all;word-wrap:break-word;font-family:Myanmar3;padding-left:45px;font-size:14px"><font color="#0000FF"><b>Receiver：</b><SPAN>'+requestList.receiverName+'</SPAN></font></th>'
  temp2+='<th colspan="4" align="left" style="padding-left:45px;font-size:14px"><font color="#0000FF"><b>Contact：</b><SPAN>'+requestList.receiverMobile+'</SPAN></font></th></tr>'
  temp2+='<tr><th colspan="7" align="left" style="word-break:break-all;word-wrap:break-word;font-family:Myanmar3;padding-left:45px;font-size:14px"><font color="#0000FF"><b>Address：</b><SPAN>'+requestList.reciverAddress+'</SPAN></font></th></tr>'
  temp2+='<tr><th colspan="7" align="left"></th></tr>'
  temp2 += '<TR><TD width="5%"><DIV align=center><b>#</b></DIV></TD>'
  temp2 += '<TD width="12%"><DIV align="center"><b>SKU</b></DIV></TD>'
  temp2 += '<TD width="30%"><DIV align="center"><b>Name</b></DIV></TD>'
  temp2 += '<TD width="20%"><DIV align="center"><b>Spec</b></DIV></TD>'
  temp2 += '<TD width="8%"><DIV align="center"><b>Qty</b></DIV></TD>'
  temp2 += '<TD width="10%"><DIV align="center"><b>Price</b></DIV></TD>'
  temp2 += '<TD width="15%"><DIV align="center"><b>Amount</b></DIV></TD>'
  temp2 += '</thead><TBODY>'
  for (let index = 0; index < requestList.printingOrderItemVOList.length; index++){
    let tempLength=requestList.printingOrderItemVOList[index].itemName.length
    let tempitemName
    if(tempLength>176)
    {
      tempitemName=requestList.printingOrderItemVOList[index].itemName.replace('\n', ' ').replace('\r', ' ').substring(0,176)+'......'
    }
    else{
      tempitemName=requestList.printingOrderItemVOList[index].itemName.replace('\n', ' ').replace('\r', ' ')
    }
    temp2+='<TR><TD align=center>' + (index+1) + '</TD><TD>'+requestList.printingOrderItemVOList[index].itemSku+'</TD>'
    temp2+='<TD style="word-break:break-all;word-wrap:break-word;font-family:Myanmar3;font-size:10px">'+tempitemName + '</TD>'
    temp2+='<TD style="word-break:break-all;word-wrap:break-word;font-family:Myanmar3;font-size:12px">' + requestList.printingOrderItemVOList[index].spec.replace('\n', ' ').replace('\r', ' ') + '</TD>'
    temp2+='<TD align=center>' + Math.trunc(requestList.printingOrderItemVOList[index].quantity) + '</TD>'
    temp2+='<TD align=center>' + Math.trunc(requestList.printingOrderItemVOList[index].unitPrice) + '</TD>'
    temp2+='<TD align=center>' + (requestList.printingOrderItemVOList[index].quantity * requestList.printingOrderItemVOList[index].unitPrice) + '</TD>'
    temp2+='</TR>'
  }
  temp2 += '<tfoot><tr><td></td><td></td><td></td><td></td><td></td><td align="center">Total</td><td tdata="subSum" format="#,##0" align="center">ks ###</td></tr></trfoot>'
  temp2 += '</TBODY></TABLE></DIV>'
  // let image = new Image()
  // image.src = require('@/assets/Speedjet.png')
  // let base64 = getBase64Image(image)

  getUrlBase64(require('@/assets/Speedjet.png'), 'png', function (imageBase64){
    let LODOP = getLodop()
    LODOP.PRINT_INIT('打印小票')
    LODOP.SET_PRINT_PAGESIZE(1, '', '', '')
    LODOP.SET_PRINT_MODE('PRINT_NOCOLLATE', 1)
    LODOP.ADD_PRINT_IMAGE(12,0,'100%',100,imageBase64)
    LODOP.SET_PRINT_STYLEA(0,'Stretch',2)
    LODOP.ADD_PRINT_TABLE(108,'0','100%',264,stemp+temp2)
    LODOP.SET_PRINT_STYLEA(0,'Vorient',3)
    LODOP.SET_PRINT_STYLEA(0,'Top2Offset',-70)
    LODOP.ADD_PRINT_HTM('97%',600, 300, 100,'<font color="#0000ff" format="Num"><span tdata="pageNO">Page ##</span>/<span tdata="pageCount"> Total ##</span></font>')
    LODOP.SET_PRINT_STYLEA(0,'ItemType',1)
    LODOP.SET_PRINT_STYLEA(0,'Horient',1)
    LODOP.PREVIEW()
    //LODOP.PRINT_DESIGN()
  })
}
//图片转换base64
export function getUrlBase64(url, ext,callback){
  let canvas = document.createElement('canvas')//创建canvas DOM元素
  let ctx = canvas.getContext('2d')
  let img = new Image
  img.crossOrigin = 'Anonymous'
  img.src = url
  img.onload = function (){
    canvas.height = img.height //指定画板的高度,自定义
    canvas.width = img.width //指定画板的宽度，自定义
    ctx.drawImage(img, 0, 0, img.width, img.height)//参数可自定义
    let dataURL = canvas.toDataURL('image/' + ext)
    callback.call(this, dataURL) //回掉函数获取Base64编码
    canvas = null
  }
}